package com.weigang.webservice;


import com.weigang.webservice.dto.User;
import com.weigang.webservice.interceptor.ClientLoginInterceptor;
import com.weigang.webservice.service.UserAuthInterceptorServer;
import com.weigang.webservice.service.UserService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;


public class WebserviceClient {




    /**
     * 不需要用户认证的webservice客户端
     */
//    public static void main(String[] args) {
//        String str = "11111111";
//        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//        factory.setAddress("http://192.168.50.111:8080/ws/userService?wsdl");
//        factory.setServiceClass(UserService.class);
//        UserService userService = factory.create(UserService.class);
//        Client client = ClientProxy.getClient(userService);
//        Endpoint cxfEndpoint = client.getEndpoint();
//        cxfEndpoint.getOutInterceptors().add(new LoggingOutInterceptor());
//        System.out.println(userService.getClass());
//        User user = userService.queryUser("sadas", str);
//        System.out.println(user);
//    }

    /**
     * 需要用户认证的webservice客户端
     */
    public static void main(String[] args) {
        try{
            String username = "admin";
            String password = "123456";
            String str = "11111111";
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.setAddress("http://192.168.50.111:2222/ws/userAuthInterceptorServer?wsdl");
            factory.setServiceClass(UserAuthInterceptorServer.class);
            UserAuthInterceptorServer userAuthInterceptorServer = factory.create(UserAuthInterceptorServer.class);
            Client client = ClientProxy.getClient(userAuthInterceptorServer);
            Endpoint cxfEndpoint = client.getEndpoint();
            cxfEndpoint.getOutInterceptors().add(new LoggingOutInterceptor());
            //用户认证
            cxfEndpoint.getOutInterceptors().add(new ClientLoginInterceptor(username, password));
            User user = userAuthInterceptorServer.queryUserAuthInterceptor(str, str);
            System.out.println(user);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

}
